
import { homeData, getUpdate, wxLogin, articleCollect, setAPIToken ,Transpond} from "../../../api/eapi.js"



/**
 *
 */
const pageConfig = {
  /**
   *
   */
  data: {
    homeData: null,
    nextPage:'',
    fetchStatus: null,
    showLoading: false,
    showError: false,
    swiperCurrent: 1,
    showRefresh: false,
    dailySign:null,
    articles: {
      items:''
    },
    patten:'',
    feature:''
  },
  /**
   * 周一要解决的问题
   * 首页的收藏与取消收藏 需要后台给个能给定指定页数的所有文章
   * 不然会导致一个逐步加载每一页 不良好体验
   *
   * 取消收藏接口，显示成功 实际未成功
   */
  onAddCollection: function(e){
    let id = e.target.dataset.id;
    let homeData = this.data.homeData;
    wx.showLoading({
      title: 'loading',
    });
    articleCollect.post({id: id}).then(res => {
      homeData.forEach(item => {
        if(item.id === id) {
          item.selected = true;
        }
      })
      // 更新选中状态
      this.setData({
        homeData: homeData
      },() => {wx.hideLoading();wx.showToast({title: '收藏成功'})})
    }).catch(err => console.error(err));


  },
  onRemoveCollection: function(e){
    let id = e.target.dataset.id;
    let homeData = this.data.homeData;
    wx.showLoading({
      title: 'loading',
    });
    articleCollect.delete({ids: id}).then(res => {

      homeData.forEach(item => {
        if(item.id === id) {
          item.selected = false;
        }
      })
      // 更新选中状态
      this.setData({
        homeData: homeData
      },() => {wx.hideLoading();wx.showToast({title: '取消收藏成功'})})
    }).catch(err => console.error(err));
  },
  onReachBottom:function(){
    let _this=this;
    getUpdate(_this.data.nextPage,_this);

  },
  onPullDownRefresh:function(){
    let _this=this;
    homeData("article",{}).then(res =>{
      let data = res.data.data.data;
      // 为文章添加已选择（已收藏）的状态字段
      _this.setData({
        patten: "",
        feature: ''
      })
      _this.setData({
        homeData: _this.setpatten(data),
        nextPage:res.data.data.next_page_url
      });
      
      homeData("today-daily-sign", {}).then(res => {
        _this.setData({
          dailySign: res.data.data
        })
      })

      wx.showToast({
        title: '刷新成功',
        icon:"none",
        duration:1000
      });
      wx.stopPullDownRefresh()
    })

  },
  /**
   * onSwiperChange
   *
   * @param e event object
   */

  /**
   * Page on Load
   */
  onLoad: function () {
    let _this = this;

    homeData("article", {}).then(res => {
      let data = res.data.data.data;
      console.log(data)
      // 为文章添加已选择（已收藏）的状态字段
     

      _this.setData({
        homeData: _this.setpatten(data),
        nextPage: res.data.data.next_page_url
      });
    })

    homeData("today-daily-sign", {}).then(res => {
      _this.setData({
        dailySign: res.data.data
      })
    })
  },
  onShow: function() {
    let _this=this;
  wx.getStorage({
    key: 'homeData',
    success: function(res) {
      if(res.data){
        homeData("article", {}).then(res => {
          let data = res.data.data.data;
         
          _this.setData({
            homeData: _this.setpatten(data),
            nextPage: res.data.data.next_page_url
          });
        });
       wx.setStorage({
         key: 'homeData',
         data: false,
       })
      }
    },
  })
      
  },
  onShareAppMessage(res){
    return Transpond(res);

  },
  setpatten(data) {

    let patten=this.data.patten,feature=this.data.feature,_this=this;

    data.forEach(item => {
      if (item.column_name == patten && item.feature == feature) {
        item.column_title = false
      } else {
       
        _this.setData({
          patten : item.column_name,
          feature : item.feature
        })
        item["column_title"] = item.column_name
      };
      item.selected = item.user_has_collect ? true : false
    })
   
  return data
  }
};

/**
 * Redux Map State to Data
 *
 * @param state
 */


/**
 * Redux Map Dispatch to Page
 *
 * @param dispatch method
 */


/**
 * Page
 */
Page(pageConfig)
